package sparrowGui
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import sparrowGui.item.Item;
	import sparrowGui.utils.PubFun;

	public class PageList extends VList
	{
		private static const BTN_JUMPPAGE:String	= "btn_jumppage";
		private static const BTN_PREV:String		= "btn_prev";
		private static const BTN_NEXT:String		= "btn_next";
		private static const BTN_LAST:String		= "btn_last";
		private static const BTN_FIRST:String		= "btn_first";
		private static const TXT_INPUTNUM:String	= "txt_inputNum";
		private static const TXT_SHOWNUM:String	= "txt_showNum";
		private static const TXT_PERNUM:String	= "txt_perNum";
		private static const COMBOBOX_PAGE:String	= "combobox_Page";
		
		// 页控件前缀
		private static const PREFIX_PAGE:String = "ListItem";
		
		// 当前页
		private  var _currentPage:int = 0;
		// 每页最多的条数,为0显示全显示完
		private var _pageNum:int	= 0;
		// 总页数,为0表示只有一页
		private var _totalNum:int = 0;
		
		private var _pageSkin:Sprite;
		
		public function PageList(data:Array, itemSkinCla:Class=null,pageSkin:Sprite=null, vars:Object=null)
		{
			vars = vars || SparrowGUI.getIns().listVar;
			
			_pageSkin = pageSkin;
			_pageNum = vars.pageNum;
			
			super(data, itemSkinCla, vars);
		}
		
		override protected function init():void
		{
			super.init();
			if(!_pageSkin) return;
			_pageSkin.addEventListener(MouseEvent.CLICK,onPageMouseHandle);
		}
		
		private function onPageMouseHandle(e:MouseEvent):void
		{
			var tName:String = e.target.name,tPageNum:int=_currentPage;
			switch(tName){
				case BTN_PREV:
				{
					if(_currentPage>0){
						tPageNum = _currentPage - 1;
					}
					break;
				}
				case BTN_NEXT:
				{
					if(_currentPage<totalPage){
						tPageNum = _currentPage + 1;
					}else tPageNum = totalPage;
					break;
				}
				case BTN_LAST:
				{
					tPageNum = totalPage;
					break;
				}
				case BTN_FIRST:
				{
					tPageNum = 0;
					break;
				}
				case BTN_JUMPPAGE:
				{
					// 有Go按钮，肯定得有一个名为'txt_inputnum'与之对应，不然报错
					var txt:TextField = _pageSkin[TXT_INPUTNUM] as TextField;
					tPageNum = int(txt.text) - 1;
					break;
				}
			}
			
			if(tName.indexOf(PREFIX_PAGE)>=0)
				tPageNum = int(PubFun.delStr(tName,PREFIX_PAGE));
			
			if(tPageNum==_currentPage) return;
			jumpPage(tPageNum);
		}
		
		override public function update(data:Object):void
		{
			super.update(data);
			
			jumpPage(_currentPage);
			if(!_pageSkin.hasOwnProperty(COMBOBOX_PAGE)) return;
			var arr:Array = [];
			for(var i:int=0;i<=totalPage;i++) arr.push(i+1);
			
			(_pageSkin[COMBOBOX_PAGE] as ComboBox).update(arr);
		}

		/**
		 * 创建列表数据.
		 */
		override protected function makeListItems():void
		{
			// 如果每页条数为0表示不分页，将数据全显示出来
			if(style.pageNum==0){
				super.makeListItems();
				return;
			}
			
			
			// 保存上次滚动条的位置
//			if(!style.isSaveSelect) _selectedIndexs = [];
//			else _scrollValue = scrollbar.value;
			
			if(style.autoSort && _itemsData && _itemsData.length) style.itemOdds = PubFun.oddsByDatas(_itemsData[0]);
			
			// 优化显示容器，如果当前的数据条数和皮肤不变，只是数据内容变了则不必清除项再重新生成，只需要将已有的项按新数据重新斌值
			_isRedrawItem = _isRedrawItem || (_pageNum!=contDP.numChildren);
			
			if(_isRedrawItem) PubFun.clearDisp(contDP);
			
			var startId:int = _currentPage*_pageNum;
			var currPageNum:int = (!_itemsData || !_itemsData.length)?0:(_currentPage==totalPage)?
				(itemsData.length-totalPage*_pageNum):_pageNum;
			
			for(var i:int=startId; i<(startId+currPageNum); i++){
				if(_isRedrawItem) createItem(_itemsData[i],i);
				else createItem(_itemsData[i],i,(contDP.getChildAt(i-startId) as Item));
			}
			while(i<(startId+_pageNum)){
				i++;
				if((i-startId-1)>=contDP.numChildren) continue;
				contDP.getChildAt(i-startId-1).visible = false;
			}
			_isRedrawItem = false;
			
			arrangeItems();
		}
		
		// 判断页码是否有效
		private function changeValidPage(pageId:int):int
		{
			if(pageId<0) return 0;
			else if(pageId>totalPage) return totalPage;
			else return pageId;
		}
		
		// 跳转到第N页
		public function jumpPage(pageId:int):void
		{
			pageId = changeValidPage(pageId);
			_currentPage = pageId;
			nextDraw();
			
			if(_pageSkin.hasOwnProperty(TXT_INPUTNUM))
				(_pageSkin[TXT_INPUTNUM] as TextField).text = String(_currentPage+1);
			
//			if(_pageSkin.hasOwnProperty(TXT_SHOWNUM))
//				(_pageSkin[TXT_SHOWNUM] as TextField).text = String(_currentPage+1);
			
			if(_pageSkin.hasOwnProperty(TXT_SHOWNUM))
				(_pageSkin[TXT_SHOWNUM] as TextField).text = String(_currentPage+1) + "/" + String(totalPage+1);
			
			dispatchEvent(new RadioEvent(RadioEvent.PAGE_REFRESH));
		}
		
		// 直接跳到某条记录，如果不在滚动范围内的话，先将页数翻到再跳转
		override public function setScrollIndex(index:int):void
		{
			index = changeValidPage(index);
			var oItem:Item = getItemByIndex(index);
			if(!oItem){
				jumpPage(int(index/_pageNum));
				oItem = getItemByIndex(index);
			}
			if(!oItem) return;
			var val:Number = (oItem.y) / (contDP.height - style.height);
			val = val>1?1:val;
			_vScroll.value = val;
			_vScroll.onMoveSlider(null);
		}
		
		private function get totalPage():int
		{
			if(!_itemsData) return 0;
			return int(itemsData.length/_pageNum-(((_itemsData.length%_pageNum)>0)?0:1));
		}

		// 设置每页的数据条数
		public function get pageNum():int
		{
			return style.pageNum;
		}

		public function set pageNum(value:int):void
		{
			style.pageNum = value;
//			_pageNum = value;
		}

		// 当前页
		public function get currentPage():int
		{
			return _currentPage;
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(_pageSkin){
				_pageSkin.removeEventListener(MouseEvent.CLICK,onPageMouseHandle);
				_pageSkin = null;
			}
		}
	}
}